package com.higgins.eventtracking.client.health.fundamentals;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class WellBeingTab extends VerticalPanel implements GenericTabInterface	{
	private final EventTrackingServiceAsync greetingService = GWT
											.create(EventTrackingService.class);
	HorizontalPanel rowOne = new HorizontalPanel();
	HorizontalPanel rowTwo = new HorizontalPanel();
	HorizontalPanel rowThree = new HorizontalPanel();
	
	private Label daysSinceLowFrameOfMind = new Label();
	private Label daysSinceLowWellBeing = new Label();
	//private Label daysSinceLowEnergy = new Label();
	private Label daysSinceSleep = new Label();
	private Label daysSinceStretch = new Label();
	private Label daysSinceDump = new Label();

	private Label blankOne = new Label();

	private boolean firstRun = true;
	
//	private Boolean alert1 = null;
//	private Boolean alert2 = null;
//	private Boolean alert3 = null;
	private Fundamentals parentTab = null;
	private String tabTitle = null;
	private Integer tabIndex = null;
	private Integer height = 20;
	
	public WellBeingTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		setHeight("20em");
		setWidth("800px");

	}
	
	public WellBeingTab()	{
		
	}
	public void update()	{
		if (firstRun)
			initialise();
	}
	public void initialise()	{
		firstRun = false;
		setLabelWidths();
		addFields();
		populateFields();
	}
	private void addFields()	{
		addRowOne();
		addRowTwo();
		addRowThree();
//		setHeight("100%");
	}
	
	private void addRowOne()	{
		rowOne.setSpacing(3);
		rowOne.add(daysSinceDump);
		setCellHeight(daysSinceDump, Integer.toString(daysSinceDump.getElement().getOffsetHeight())+"px");
		rowOne.add(daysSinceLowFrameOfMind);
		setCellHeight(daysSinceLowFrameOfMind, Integer.toString(daysSinceLowFrameOfMind.getElement().getOffsetHeight())+"px");
		add(rowOne);
	}
	private void addRowTwo()	{
		rowTwo.setSpacing(3);
		rowTwo.add(daysSinceSleep);
		setCellHeight(daysSinceSleep, Integer.toString(daysSinceSleep.getElement().getOffsetHeight())+"px");
		rowTwo.add(daysSinceLowWellBeing);
		setCellHeight(daysSinceLowWellBeing, Integer.toString(daysSinceLowWellBeing.getElement().getOffsetHeight())+"px");
		add(rowTwo);
	}
	private void addRowThree()	{
		rowThree.setSpacing(3);
		rowThree.add(daysSinceStretch);
		setCellHeight(daysSinceStretch, Integer.toString(daysSinceStretch.getElement().getOffsetHeight())+"px");
		//rowThree.add(daysSinceLowEnergy);
		//setCellHeight(daysSinceLowEnergy, Integer.toString(daysSinceLowEnergy.getElement().getOffsetHeight())+"px");
		add(rowThree);
	}
	private void setLabelWidths()	{
		daysSinceLowFrameOfMind.setWidth("250px");
		daysSinceLowWellBeing.setWidth("250px");
		//daysSinceLowEnergy.setWidth("250px");
		daysSinceDump.setWidth("250px");
		daysSinceSleep.setWidth("250px");
		daysSinceStretch.setWidth("250px");
		blankOne.setWidth("250px");
	}
	private void setLabelHeight()	{
		daysSinceLowFrameOfMind.setHeight(height + "px");
		daysSinceLowWellBeing.setHeight(height + "px");
		//daysSinceLowEnergy.setWidth("250px");
		daysSinceDump.setHeight(height + "px");
		daysSinceSleep.setHeight(height + "px");
		daysSinceStretch.setHeight(height + "px");
		blankOne.setHeight(height + "px");
	}
	private void populateFields()	{
		populateDaysSinceLowFrameOfMind();
		populateDaysSinceLowWellBeing();
		//populateDaysSinceLowEnergy();
		populateDaysSinceDump();
		populateDaysSinceSleep();
		populateDaysSinceStretch();
	}
	private void populateDaysSinceLowFrameOfMind()	{
		greetingService.retrieveDaysSinceLowFrameOfMind(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						daysSinceLowFrameOfMind.setText(results.toString() + " days since last low frame of mind");
					}
				});
	}
	private void populateDaysSinceLowWellBeing()	{
		greetingService.retrieveDaysSinceLowWellBeing(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						daysSinceLowWellBeing.setText(results.toString() + " days since last low well being");
					}
				});
	}
	/*
	private void populateDaysSinceLowEnergy()	{
		greetingService.retrieveDaysSinceLowEnergy(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						daysSinceLowEnergy.setText(results.toString() + " days since last low energy");
					}
				});
	}	*/
	private void populateDaysSinceSleep()	{
		greetingService.retrieveDaysSinceSleep(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert1 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						daysSinceSleep.setText(results.toString() + " days since last sleep session");
						if (results!=null && results > 6)	{
							daysSinceSleep.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert1 = true;
//						} else {
//							alert1 = false;
						}
//						passBackToParent();

					}
				});
	}
	private void populateDaysSinceStretch()	{
		greetingService.retrieveDaysSinceStretch(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert2 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						daysSinceStretch.setText(results.toString() + " days since last stretch");
						if (results!=null && results > 3)	{
							daysSinceStretch.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert2 = true;
//						} else {
//							alert2 = false;
						}
//						passBackToParent();

					}
				});
	}
	private void populateDaysSinceDump()	{
		greetingService.retrieveDaysSinceDump(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
//						alert3 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						daysSinceDump.setText(results.toString() + " days since last dump");
						if (results!=null && results > 3)	{
							daysSinceDump.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
//							alert3 = true;
//						} else {
//							alert3 = false;
						}	
//						passBackToParent();
					}
				});
	}
//	private void passBackToParent()	{
//		if (alert1!=null && alert2!=null && alert3!=null)	{
//			parentTab.addWellBeingTab(alert1||alert2||alert3);
//		}
//	}

	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}

	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}

}
